<template>
	<view>
		<!-- 自定义导航 -->
		<uni-nav-bar
			left-icon="back"
			@clickLeft="backTo()"
			backgroundColor="#ffefd6"
			:border="false" statusBar
		></uni-nav-bar>
		<view class="title">
			<view>你好，</view>
			<view>欢迎来到xx小说</view>
		</view>
		<view class="tologin">
			<view class="message">
				<!-- 手机号 -->
				<view>
					<view>手机号</view>
					<input
						type="number"
						v-model="phone"
						maxlength="11"
						placeholder="请输入手机号"
						placeholder-style="font-size:28rpx"
						@input="checkPhone()"
					/>
					<view v-show="phoneState">
						<uni-icons v-show="phoneState == 'n'" type="clear" color="red" size="24"></uni-icons>
						<uni-icons v-show="phoneState == 'y'" type="checkbox-filled" color="green" size="24"></uni-icons>
					</view>
				</view>
				<!-- 邮箱 -->
				<view>
					<view>邮箱</view>
					<input
						type="text"
						v-model="email"
						placeholder="请输入邮箱"
						placeholder-style="font-size:28rpx"
						@input="checkEmail()"
					/>
					<view v-show="emailState">
						<uni-icons v-show="emailState == 'n'" type="clear" color="red" size="24"></uni-icons>
						<uni-icons v-show="emailState == 'y'" type="checkbox-filled" color="green" size="24"></uni-icons>
					</view>
				</view>
				<!-- 验证码 -->
				<view class="captcha">
					<view>
						<view>验证码</view>
						<input
							type="number"
							v-model="capt"
							placeholder="请输入验证码"
							placeholder-style="font-size:28rpx"
						/>
					</view>
					<view>
						<view v-html="svg" @click="getSvg()"></view>
						<view>看不清?点击刷新</view>
					</view>
				</view>
				<!-- 密码 -->
				<view>
					<view>密码</view>
					<!-- <input type="password" value="" placeholder="请输入密码" /> -->
					<input
						maxlength="15"
						v-model="upwd"
						class="uni-input"
						password
						type="text"
						placeholder-style="font-size:28rpx"
						placeholder="请输入6-15位密码"
						@input="checkUpwd()"
					/>
					<view v-show="upwdState">
						<uni-icons v-show="upwdState == 'n'" type="clear" color="red" size="24"></uni-icons>
						<uni-icons v-show="upwdState == 'y'" type="checkbox-filled" color="green" size="24"></uni-icons>
					</view>
				</view>
			</view>
			<view class="btn-login" @click="toRegist()">注册</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			phone: '',
			phoneState: '',
			email: '',
			emailState: '',
			upwd: '',
			upwdState: '',
			capt: '',
			svg: '',
			answer: '',
			timer: null
		};
	},
	methods: {
		// 返回上一页
		backTo() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 获取svg验证码
		getSvg() {
			uni.request({
				url: `${getApp().globalData.baseURL}/v1/user/captcha`,
				success: res => {
					console.log(res);
					this.svg = res.data.svg;
					this.answer = res.data.text;
				}
			});
		},
		// 验证手机号码格式
		checkPhone() {
			if (this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(() => {
				console.log(this.phone);
				let reg = /^1[3-9]\d{9}$/;
				this.phoneState = reg.test(this.phone.trim()) ? 'y' : 'n'; // ....
			}, 250);
		},
		// 验证邮箱格式
		checkEmail() {
			if (this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(() => {
				this.emailState = /^\w+@\w+\.\w+(\.cn)?$/.test(this.email.trim()) ? 'y' : 'n';
			}, 250);
		},
		// 验证密码位数
		checkUpwd() {
			if (this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(() => {
				this.upwdState = this.upwd.length >= 6 && this.upwd.length <= 15 ? 'y' : 'n';
			}, 250);
		},
		// 注册
		toRegist() {
			let { capt, answer, phone, email, upwd, phoneState, emailState, upwdState } = this;
			let vm = this;
			// 没有输入信息提示用户输入信息再注册
			if (!phone.trim() || !email.trim() || !upwd.trim()) {
				uni.showToast({
					title: '请输入用户信息',
					icon: 'none',
					duration: 1000
				});
				return;
			}
			if (capt != answer) {
				this.getSvg();
				uni.showToast({
					title: '验证码错误请重新输入',
					icon: 'none',
					duration: 300
				});
				this.capt = '';
				return;
			}
			// 输入格式均正确 则进行注册
			if (phoneState && emailState && upwdState == 'y') {
				let uname = `用户${Date.now()}`; //生成用户名唯一标识
				console.log(uname);
				uni.request({
					url: `${getApp().globalData.baseURL}/v1/user/reg`,
					data: {
						uname,
						upwd,
						phone,
						email
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded' // application/x-www-form-urlencoded
					},
					success: res => {
						console.log(res);
						if (res.data.code == 201) {
							// 用户已注册
							uni.showModal({
								content: '用户已存在，去登录',
								confirmColor:"#ff604f",
								success: function(res) {
									if (res.confirm) {
										uni.redirectTo({
											url: '../login/login'
										});
									} else if (res.cancel) {
										// console.log('用户点击取消');
										vm.phone = vm.email = vm.capt = vm.upwd = vm.phoneState = vm.emailState = vm.upwdState =
											'';
										//重新获取验证码 
										vm.getSvg();
									}
								}
							});
						} else if (res.data.code == 200) {
							// 注册成功  注册+登录
							
							let user=res.data.data[0]
							// 存全部数据 全局vuex+本地缓存
							this.$store.commit('updateUser',user);
							// console.log(this.$store.state.user)
							// 登陆状态修改为true
							this.$store.commit('toLogin',true)
							// toast 显示
							uni.showToast({
								title: '注册成功',
								icon: 'none',
								duration: 300
							});
							// 返回之前页面 上2
							setTimeout(()=>{
								uni.navigateBack({
									delta: 2
								});
							},500)
							
							// // 跳转到tab页我的
							// setTimeout(() => {
							// 	uni.switchTab({
							// 		url: '../me/me'
							// 	});
							// }, 500);
						}
					}
				});
			}
		}
	},
	mounted() {
		this.getSvg();
	}
};
</script>

<style lang="scss" scoped>
.title {
	padding: 40rpx 40rpx 100rpx;
	font-size: 44rpx;
	background-color: #ffefd6;
	> view {
		margin-top: 20rpx;
	}
}
.tologin {
	margin-top: -30rpx;
	padding: 50rpx;
	background-color: #fff;
	border-radius: 40rpx;
	.message {
		width: 100%;
		padding-top: 30rpx;
		> view {
			padding: 30rpx 10rpx;
			display: flex;
			// justify-content: space-between;
			flex: 1;
			border-bottom: 1px solid #999;
			> view:first-child {
				width: 140rpx;
				font-size: 32rpx;
			}
			> input {
				width: 440rpx;
			}
		}
		.captcha {
			border-bottom: 1px solid #999;
			display: flex;
			// justify-content: space-between;
			align-items: center;
			padding: 0 10rpx;
			> view:first-child {
				flex: 1;
				display: flex;
				padding: 30rpx 0;
				> view {
					width: 140rpx;
					font-size: 32rpx;
				}
				> input {
					width: 240rpx;
				}
			}
			> view:last-child {
				> view:last-child {
					font-size: 22rpx;
					color: #888;
					text-align: center;
				}
			}
		}
	}
}
.btn-login {
	padding: 20rpx 16rpx;
	margin-top: 80rpx;
	border-radius: 44rpx;
	text-align: center;
	background-color: #ff604f;
	color: #fff;
	font-size: 38rpx;
	letter-spacing: 30rpx;
}
</style>
